/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmViewList.java
 *
 * Created on Jul 30, 2009, 9:42:23 PM
 */
package cconverter;

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import org.jdesktop.application.Action;

/**
 *
 * @author SapunBoj
 */
public class FrmViewList extends javax.swing.JFrame {

    JFrame _frm = this;

    /** Creates new form FrmViewList */
    public FrmViewList() {
        initComponents();
        myInit();
        setTable();
        this.pack();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlMain = new javax.swing.JPanel();
        lblLastUpdate = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        btnClose = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cconverter.CConverterApp.class).getContext().getResourceMap(FrmViewList.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        pnlMain.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("pnlMain.border.title"))); // NOI18N
        pnlMain.setName("pnlMain"); // NOI18N

        lblLastUpdate.setText(resourceMap.getString("lblLastUpdate.text")); // NOI18N
        lblLastUpdate.setName("lblLastUpdate"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "...", "Currency", "Rate"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        table.setFillsViewportHeight(true);
        table.setName("table"); // NOI18N
        jScrollPane1.setViewportView(table);
        table.getColumnModel().getColumn(0).setResizable(false);
        table.getColumnModel().getColumn(0).setPreferredWidth(18);
        table.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("table.columnModel.title0")); // NOI18N
        table.getColumnModel().getColumn(1).setPreferredWidth(120);
        table.getColumnModel().getColumn(1).setHeaderValue(resourceMap.getString("table.columnModel.title1")); // NOI18N
        table.getColumnModel().getColumn(2).setHeaderValue(resourceMap.getString("table.columnModel.title2")); // NOI18N

        javax.swing.GroupLayout pnlMainLayout = new javax.swing.GroupLayout(pnlMain);
        pnlMain.setLayout(pnlMainLayout);
        pnlMainLayout.setHorizontalGroup(
            pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMainLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                    .addComponent(lblLastUpdate))
                .addContainerGap())
        );
        pnlMainLayout.setVerticalGroup(
            pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlMainLayout.createSequentialGroup()
                .addComponent(lblLastUpdate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 404, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cconverter.CConverterApp.class).getContext().getActionMap(FrmViewList.class, this);
        btnClose.setAction(actionMap.get("Exit")); // NOI18N
        btnClose.setText(resourceMap.getString("btnClose.text")); // NOI18N
        btnClose.setName("btnClose"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(pnlMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnClose)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblLastUpdate;
    private javax.swing.JPanel pnlMain;
    private javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables

    private void myInit() {
        _frm.setResizable(false);
        //show last update
        if (new Pxml().checkFile()) {
            try {
                lblLastUpdate.setText("Last update : " + new Pxml().getLastUpdate());
            } catch (Exception ex) {
                new FrmInfo(this, true).setInfo("Error", "Error reading file : " + ex.getLocalizedMessage());
            }
        } else {
            lblLastUpdate.setText("Last update : Never");
        }
        Maps _maps = new Maps();
        try {
            _maps.fillCMap();
            _maps.fillVMap();
        } catch (Exception ex) {
            new FrmInfo(this, true).setInfo("Error", ex.getLocalizedMessage());
        }
        if (Maps._cmap.size() > 0) {// if exist list
            ArrayList<String> all = new ArrayList<String>();
            all.addAll(Maps._vmap.keySet());
            Collections.sort(all);
            // define table model
            DefaultTableModel dtm = (DefaultTableModel) table.getModel();
            // loop for add all element in map
            for (String currency : all) {
                if (currency.length() > 0) {
                    Object[] line = new Object[3];
                    line[0] = new Flags().Flags(currency.toLowerCase());
                    line[1] = Maps._cmap.get(currency) + " (" + currency + ")";
                    line[2] = Double.parseDouble(Maps._vmap.get(currency));
                    dtm.addRow(line);
                }
            }
            table.setModel(dtm);
            TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(table.getModel());
            table.setRowSorter(sorter);
            table.setRowHeight(14);
        }
        _frm.setLocationRelativeTo(null);
        _frm.setIconImage(Sets.icon);
        _frm.pack();
    }

    @Action
    public void Exit() {
        _frm.dispose();
    }

    private void setTable() {
        DefaultTableModel dtm = (DefaultTableModel) table.getModel();
        for (int i = 0; i < dtm.getColumnCount(); i++) {
            table.getColumnModel().getColumn(i).setCellRenderer(new myRender());
            table.getColumnModel().getColumn(i).setCellRenderer(new iconRender());
        }
        // now, set table row height
        table.setRowHeight(22);
        table.setGridColor(Sets.grid);
        table.setShowHorizontalLines(true);
        table.setShowVerticalLines(false);
        table.setIntercellSpacing(new Dimension(0, 1));
    }
}
